//
//  BIZAddQuetionVC.m
//  biz
//
//  Created by 小龙 on 2017/3/21.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZAddQuetionVC.h"
#import "BWHTextView.h"
#import "MXNavigationBarManager.h"

@interface BIZAddQuetionVC ()

@property (strong, nonatomic)  BWHTextView *questionTextView;

@end

@implementation BIZAddQuetionVC {
    /** uuid */
    NSString *_uuid;
    NSString *_url;
    void(^_completion)();
}

- (instancetype)initWithUUID:(NSString *)uuid url:(NSString *)urlStr completion:(void (^)())completion {
    self = [super init];
    if (self) {
        _uuid = uuid;
        _url = urlStr;
        _completion = completion;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.view addSubview:self.questionTextView];
    self.questionTextView.placeHolder = @"请详细描述您的问题......";
    [self setupNavBar];
    
}


- (void)setupNavBar
{
    self.title = @"咨询问题";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon-denglu-fanhui副本"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItemDidClicked:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"发布" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemClick)];
    
}

- (void)leftBarButtonItemDidClicked:(UIBarButtonItem *)left
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)rightBarButtonItemClick
{
    if (!self.questionTextView.text || !self.questionTextView.text.length) {
        [MBProgressHUD showTipMessageInWindow:@"请详细描述您的问题"];
        return;
    }
    
    NSMutableDictionary *parmaDic = [NSMutableDictionary dictionary];
    parmaDic[@"uuid"] = _uuid;
    parmaDic[@"question"] = self.questionTextView.text;
    
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:_url
           parameters:parmaDic
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  DeLog(@"追问---------:%@", responseObject);
                  
                  NSInteger code = [responseObject[@"code"] integerValue];
                  NSString *des = responseObject[@"des"];
                  if (code == 1) {
                      [MBProgressHUD showSuccessMessage:des];
                      if (_completion) {
                          _completion();
                      }
                      [self dismissViewControllerAnimated:YES completion:nil];
                  }
                  else {
                      [MBProgressHUD showErrorMessage:des];
                  }
                 
              } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  DeLog(@"error---------:%@",error);
                  if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
                      [MBProgressHUD showErrorMessage:@"网络不给力"];
                  }
                  else {
                      [MBProgressHUD showErrorMessage:@"服务器繁忙"];
                  }

       }];
}

#pragma mark -- Lazyload

- (BWHTextView *)questionTextView {
    if (!_questionTextView) {
        _questionTextView = [[BWHTextView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 128)];
        _questionTextView.placeHolderTextColor = [UIColor zfw_ColorWithHex:0x999999 alpha:1.0];
        _questionTextView.font = [UIFont systemFontOfSize:14];
    }
    return _questionTextView;
}

@end
